package com.chat.websocket.util.ip2region;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * SunJingxuan
 * 2022 \ 08 \ 01
 */
public class IpAddressUtil {

	public static String getMacAddress(String ip) throws IOException {
		String macAddr = null;
		Process process = Runtime.getRuntime().exec("nbtstat -a " + ip);
		BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
		Pattern pattern = Pattern.compile("([A-F0-9]{2}-){5}[A-F0-9]{2}");
		Matcher matcher;
		for (String strLine = br.readLine(); strLine != null;
		     strLine = br.readLine()) {
			matcher = pattern.matcher(strLine);
			if (matcher.find()) {
				macAddr = matcher.group();
				break;
			}
		}
		return macAddr;
	}
}
